﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ToDoList.Entity.Models;

namespace ToDoList.Logic
{
    public class ItemsMethod:IDisposable
    {
        private ToDoListDataContext _ctx = new ToDoListDataContext();
        /// <summary>
        /// 添加备注
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="listId"></param>
        /// <param name="eventId"></param>
        /// <returns></returns>
        public bool AddRemarks(int listId, int eventId, string newRemaks)
        {
            var ListId = _ctx.ListData.SingleOrDefault(list => list.ListId == listId);
            var EventId = _ctx.ItemsData.SingleOrDefault(items => items.EventId == eventId);
            if (ListId == null && EventId == null)
            {
                return false;
            }
            else
            {
                var item = _ctx.ItemsData.Find(eventId);
                item.Remarks = newRemaks;
                _ctx.SaveChanges();
                return true;
            }
        }
        /// <summary>
        /// 添加过期时间
        /// </summary>
        /// <param name="listId"></param>
        /// <param name="eventId"></param>
        /// <param name="newOverdueEvent"></param>
        /// <returns></returns>
        public bool AddOverdueEvent(int listId, int eventId, string newOverdueEvent)
        {
            var ListId = _ctx.ListData.SingleOrDefault(list => list.ListId == listId);
            var EventId = _ctx.ItemsData.SingleOrDefault(items => items.EventId == eventId);
            if (ListId == null && EventId == null)
            {
                return false;
            }
            else
            {
                var item = _ctx.ItemsData.Find(eventId);
                item.OverdueEvent = DateTime.Parse(newOverdueEvent);
                _ctx.SaveChanges();
                return true;
            }
        }
        /// <summary>
        /// 完成事项
        /// </summary>
        /// <param name="listId"></param>
        /// <param name="eventId"></param>
        /// <returns></returns>
        public bool CompletionItems(int listId, int eventId)
        {
            var ListId = _ctx.ListData.SingleOrDefault(list => list.ListId == listId);
            var EventId = _ctx.ItemsData.SingleOrDefault(items => items.EventId == eventId);
            if (ListId == null && EventId == null)
            {
                return false;
            }
            else
            {
                var item = _ctx.ItemsData.Find(eventId);
                item.State = true;
                _ctx.SaveChanges();
                return true;
            }
        }
        /// <summary>
        /// 取消完成
        /// </summary>
        /// <param name="listId"></param>
        /// <param name="eventId"></param>
        /// <returns></returns>
        public bool Cancel(int listId, int eventId)
        {
            var ListId = _ctx.ListData.SingleOrDefault(list => list.ListId == listId);
            var EventId = _ctx.ItemsData.SingleOrDefault(items => items.EventId == eventId);
            if (ListId == null && EventId == null)
            {
                return false;
            }
            else
            {
                var item = _ctx.ItemsData.Find(eventId);
                item.State = false;
                _ctx.SaveChanges();
                return true;
            }
        }
        /// <summary>
        /// 浏览清单下是事项
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="listId"></param>
        /// <returns></returns>
        public List<ItemsData> ViewItems(int userId, int listId)
        {
            var user = _ctx.UsersData.SingleOrDefault(a => a.UserId == userId);
            var items = _ctx.ItemsData.Where(id => id.ListId == listId).ToList();
            if (user != null && items != null)
            {
                return items;
            }
            else
            {
                return null;
            }

        }

        public void Dispose()
        {
            _ctx.Dispose();
        }
    }
}
